<template>
  <screenCosts v-if="pageParams.feeCategory != 4" @listRefresh="getScreenDetail" :pageFrom="'car'"></screenCosts>
  <view v-if="pageParams.feeCategory != 4" style="height: 86rpx"></view>
  <block v-if="dataSource.length">
    <FeeRecord
      :feeCategory="pageParams.feeCategory"
      :feeData="orderDetail"
      v-for="record in dataSource"
      :record="record"
      :key="record.id"
    ></FeeRecord>
  </block>
  <block v-else>
    <ve-empty text="暂无数据"></ve-empty>
  </block>

  <ve-bottom-box size="mini">
    <ve-button type="primary" @click="toAddFee" size="long">添加费用</ve-button>
  </ve-bottom-box>
</template>

<script setup>
  import FeeRecord from './fee-record.vue'
  import screenCosts from '@/pages/order/components/screenCost.vue'

  import { provide, ref, reactive } from 'vue'
  import { getAssetFeeList } from '@/api/asset/asset-fee'
  import { getViolationDetail } from '@/api/asset/car-violation'
  import { onLoad, onShow } from '@dcloudio/uni-app'

  const screenTypes = reactive({
    feeType: '',
    costType: '',
    feeStatus: '',
  }) //筛选参数

  // 费用分类(1保养 2维修 3年检 4违章)
  const pageParams = ref()

  const orderDetail = ref({})
  const dataSource = ref([])

  const getCostList = () => {
    return new Promise((resolve) => {
      if (pageParams.value.feeCategory == 4) {
        getViolationDetail({ id: pageParams.value.businesId }).then((res) => {
          orderDetail.value = res.result
          dataSource.value = res.result.feeList
          resolve(res.result.feeList)
        })
      } else {
        getAssetFeeList({ ...pageParams.value }).then((res) => {
          var dataOne = []

          if (res.result && res.result.length) {
            res.result.forEach((i) => {
              if (
                (screenTypes.feeType == '' || screenTypes.feeType == i.feeType) &&
                (screenTypes.costType == '' || screenTypes.costType == i.costType) &&
                (screenTypes.feeStatus == '' || screenTypes.feeStatus == i.feeStatus)
              ) {
                dataOne.push(i)
              }
            })
          }
          dataSource.value = dataOne
          resolve(dataOne)
        })
      }
    })
  }

  const loadData = () => {
    getCostList().then((data) => {
      console.log('data', data)
    })
  }

  // 去添加费用
  const toAddFee = () => {
    const p_parms = pageParams.value
    uni.navigateTo({
      url: `/pages/asset/asset-fee/edit-fee?feeCategory=${p_parms.feeCategory}&businesId=${p_parms.businesId}&addorEdit=0`,
    })
  }

  // 筛选
  const getScreenDetail = (chooseTypes) => {
    screenTypes.feeType = chooseTypes.feeType
    screenTypes.costType = chooseTypes.costType
    screenTypes.feeStatus = chooseTypes.feeStatus

    loadData()
  }

  onLoad(({ feeCategory, businesId }) => {
    pageParams.value = { feeCategory, businesId }
    provide(pageParams)
    // loadData()

    // // 刷新列表
    // uni.$on('refreshData', () => {
    //   loadData()
    // })
  })

  onShow(() => {
    console.log('onShow')

    loadData()
  })
</script>

<style lang="less" scoped></style>
